#!/bin/csh
# 2d_movie.csh
#########################################################################
# The scripts reads all grd_****_.dat files contained in $dirout
# and runs ./2d_plots.csh for each of them
#
# - Set the simulation directory containing YACC's output 
#   in the file "simulation.dir"
# - Run the script using:
#   ./2d_movie.csh field  xmin xmax zmin zmax
#   - 'field' is the field to plot. Choose among:
#      temperature, tempanomaly, tbuoyancy, velocity, viscosity, viscdiss, 
#      composition, companomaly, conductivity, expansivity, heats
#   - 'xmin', 'xmax', 'zmin', 'zmax' (optional) are the coordinates of the 
#     domain limits within which the contour plots are needed. If nothing is provided: 
#     xmin=0, xmax=aspect ratio, zmin=0, zmax=1, otherwise all of them must be set
########################################################################################

#--------------------------------------------------------------------------------
# Options:
#--------------------------------------------------------------------------------
# dirout: directory of the YACC's output files (needs to be written in the 
#         'simulation.dir' file)
# dirplots: directory where to store the plots (default: $dirout/Plots/$field)
# makemovie: if 1 an animated gif is produced 
#            N.B.: makejpg must be 1 in 2d_plots and the directory
#            $dirplots$field must contain only the jpg files 
#            to be used to make the movie
# tstepmax: output index of the last time step that needs to be plotted (default 999999)
#---------------------------------------------------------------------------------------

source simulation.dir

if (! -d $dirout) then
  echo $dirout 'does not exist. Check the path stored in the variable "dirout" '
  exit
endif

set dirplots = {$dirout}Plots/ 

set makemovie = 0

set tstepmax = 999999
set tstepmax = `echo $tstepmax | awk '{print $1 + 0}'`

#------------------------------------------------------
# Read the field to plot from standard input
#------------------------------------------------------
set field = ${1}
if (${1} !~ temperature && ${1} !~ tempanomaly && ${1} !~ tbuoyancy && ${1} !~ velocity && ${1} !~ viscosity && \
    ${1} !~ viscdiss && ${1} !~ composition && ${1} !~ companomaly && ${1} !~ conductivity && ${1} !~ expansivity \
    && ${1} !~ heats && ${1} !~ depletion && ${1} !~ solidus && ${1} !~ strain && ${1} !~ defmech) then
  echo 'The field you want to plot is either not (yet) supported in this routine or was misspelled'
  exit
endif

if (${2} != "" && ${3} == "" || ${3} != "" && ${4} == "" || ${4} != "" && ${5} == "") then
  echo 'To plot only part of the domain, xmin, xmax, zmin and zmax must all be set.'
  exit
endif

#------------------------------------------------------
# Run 2d_plots on all grd files in $dirout
#------------------------------------------------------
if (! -d $dirplots) then
  mkdir $dirplots
endif
if (! -d $dirplots$field) then
  mkdir $dirplots$field
endif

set files = (`ls ${dirout}grd*.dat | grep -v "grd_restart*"`)

foreach file ($files)

   set nchar = `echo $file | awk '{print length($0)}'`
   set n1 = `echo "scale=1; $nchar - 10" | bc -l`
   set n2 = `echo "scale=1; $nchar - 5" | bc -l`

   set tstep = `echo $file | cut -c ${n1}-${n2}`
   set tstepcut = `echo $tstep | awk '{print $1 + 0}'`
   
   # run the script only if the current plot (jpg or pdf) doesn't already exist
   if (! -e ${dirplots}${field}/${field}_cplot_${tstep}.jpg && ! -e ${dirplots}${field}/${field}_cplot_${tstep}.pdf && ${tstepcut} <= ${tstepmax}) then
     ./2d_plots.csh $tstep $field ${2} ${3} ${4} ${5}
     echo ""
   endif
   
end

if ($makemovie == 1) then
  cd $dirplots$field
  echo "Running ImageMagick to make a gif movie....: " $file
  convert -delay 15 ${field}_cplot_*.jpg ${field}_movie.gif
endif





